package com.hzhk.happyrent.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;


import com.hzhk.happyrent.utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {

    protected List<T> mList;
    protected int index = -1;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    protected LayoutInflater mInflater;
    protected Context mContext;

    public BaseAdapter(Context context) {
        mList = new ArrayList<T>();
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<T> list) {
        if (!ListUtils.isEmpty(list)) {
            mList.addAll(list);
        }
    }

    public void setList(List<T> list) {
        mList.clear();
        if (list != null) {
            mList.addAll(list);
        }
        this.notifyDataSetChanged();
    }

    public void setList(List<T> list, int index) {
        mList = list;
        this.index = index;
        this.notifyDataSetChanged();
    }

    public void addAllDataAndNorify(List<T> list) {
        this.addAllData(list);
        this.notifyDataSetChanged();
    }

    public List<T> getData() {
        return mList;
    }

    @Override
    public int getCount() {
        if (ListUtils.isEmpty(mList))
            return 0;
        return mList.size();
    }

    @Override
    public T getItem(int position) {
        if (ListUtils.isEmpty(mList)) {
            return null;
        }
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    protected String getString(int id) {
        return mContext.getResources().getString(id);
    }

}
